package com.hk.kindheartedness;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.hk.kindheartedness.db.DBOpenHelper;
import com.hk.kindheartedness.model.DoctorDetails;
import com.hk.kindheartedness.model.User;
import com.hk.kindheartedness.model.vo.DoctorVo;
import com.hk.kindheartedness.util.ToastUtil;

/**
 * 医生个人信息业务处理
 */
public class DoctorPersonActivity extends AppCompatActivity implements View.OnClickListener {

    //    Initialize Toast
    private Toast toast;

    //    Initialize intent
    private Intent intent;

    //    声明DBOpenHelper对象
    private DBOpenHelper dbOpenHelper;

    private TextView loginPhone;

    private Button personInfoBtn,updatePwdBtn,logoutBtn;

    private String loginPhone2;

    @SuppressLint("LongLogTag")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_doctor_person);
//        实例化
        dbOpenHelper = new DBOpenHelper(DoctorPersonActivity.this, "db_kindheartedness", null, 1);
        //      提取出登录用户名
        Intent intent2 = getIntent();
        Bundle bundle = intent2.getExtras();
        loginPhone2 = bundle.getString("loginPhone");
        loginPhone = findViewById(R.id.loginPhone);
        loginPhone.setText(loginPhone2);
//        获取按钮控制权
        personInfoBtn = findViewById(R.id.personInfoBtn);
        updatePwdBtn = findViewById(R.id.updatePwdBtn);
        logoutBtn = findViewById(R.id.logoutBtn);
        personInfoBtn.setOnClickListener(this);
        updatePwdBtn.setOnClickListener(this);
        logoutBtn.setOnClickListener(this);


    }

    /**
     * 点击按钮事件
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
//            查看个人资料
            case R.id.personInfoBtn:
//                回显
                findByPhonePerson();
                break;
//                修改密码
            case R.id.updatePwdBtn:
                findByPhone();
                break;
//                退出登录
            case R.id.logoutBtn:
                intent = new Intent(DoctorPersonActivity.this,LoginActivity.class);
                startActivity(intent);
                DoctorPersonActivity.this.finish();
                break;
            default:
                return;
        }
    }

    /**
     * 通过登陆手机号获取用户个人信息
     */
    private void findByPhonePerson() {
        String loginPhoneToString = loginPhone.getText().toString();
        SQLiteDatabase readableDatabase = dbOpenHelper.getReadableDatabase();
        Cursor queryUser = readableDatabase.query("user", null, "phone = ?", new String[]{loginPhoneToString}, null, null, null);
        DoctorVo doctorVo = null;
        while (queryUser.moveToNext()) {
            String username = queryUser.getString(1);
            String phone = queryUser.getString(2);
            String sex = queryUser.getString(6);
            String age = queryUser.getString(7);
            String idNumber = queryUser.getString(8);
            String address = queryUser.getString(9);
            String doctorId = queryUser.getString(10);
//            通过构造函数进行封装
            doctorVo = new DoctorVo(Integer.parseInt(doctorId),username,phone,Integer.parseInt(sex),Integer.parseInt(age),idNumber,address);
        }
        if (doctorVo == null) {
            toast = ToastUtil.toastHint(toast,DoctorPersonActivity.this,"未查到当前登录账号个人信息,请稍后重试！");
            return;
        }
//        提取出医生ID查询医生详情
        Integer doctorId = doctorVo.getDoctorId();
        Cursor queryDoctor = readableDatabase.query("doctor_details", null, "id = ?", new String[]{doctorId.toString()}, null, null, null);
        DoctorDetails doctorDetails = null;
        while (queryDoctor.moveToNext()) {
            String positionId = queryDoctor.getString(2);
            String departmentsId = queryDoctor.getString(3);
            String specialty = queryDoctor.getString(4);
            String describe = queryDoctor.getString(5);
//            进行参数封装
            doctorDetails = new DoctorDetails();
            doctorDetails.setPositionId(Integer.valueOf(positionId));
            doctorDetails.setDepartmentsId(Integer.valueOf(departmentsId));
            doctorDetails.setSpecialty(specialty);
            doctorDetails.setDescribe(describe);
        }
        if (doctorDetails == null) {
            toast = ToastUtil.toastHint(toast,DoctorPersonActivity.this,"未查到当前登录医生个人信息,请稍后重试！");
            return;
        }
//        提取出所属科室和职位ID进行查询
        Integer positionId = doctorDetails.getPositionId();
        Integer departmentsId = doctorDetails.getDepartmentsId();
        String positionName = null,departmentsName = null;
        if (positionId != null && departmentsId != null) {
//            根据职务ID查询医生职务
            Cursor queryPosition = readableDatabase.query("position", null, "id = ? ", new String[]{positionId.toString()}, null, null, null);
            while (queryPosition.moveToNext()) {
                positionName = queryPosition.getString(1);
            }
//              根据科室ID查询医生所在科室名称
            Cursor queryDepartments = readableDatabase.query("departments", null, "id = ? ", new String[]{departmentsId.toString()}, null, null, null);
            while (queryDepartments.moveToNext()) {
                departmentsName = queryDepartments.getString(1);
            }
        } else {
            toast = ToastUtil.toastHint(toast, DoctorPersonActivity.this, "暂时没有找到医生对应职务或所属科室，请稍后查看！");
            return;
        }
//        提取出擅长和简介并进行封装
        String specialty = doctorDetails.getSpecialty();
        String describe = doctorDetails.getDescribe();
        doctorVo.setPositionName(positionName);
        doctorVo.setDepartmentsName(departmentsName);
        doctorVo.setSpecialty(specialty);
        doctorVo.setDescribe(describe);
        intent = new Intent(DoctorPersonActivity.this,DoctorPersonInfoActivity.class);
        String toJSONString = JSON.toJSONString(doctorVo);
        Bundle bundle = new Bundle();
        bundle.putString("doctorInfo",toJSONString);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    /**
     * 通过登录手机号查询个人密码
     */
    private void findByPhone() {
        String loginPhoneToString = loginPhone.getText().toString();
        intent = new Intent(DoctorPersonActivity.this,PatientUpdatePwdActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("loginPhone",loginPhoneToString);
        intent.putExtras(bundle);
        startActivity(intent);
    }


    /**
     * 断开数据库连接
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (dbOpenHelper != null) {
            dbOpenHelper.close();
        }
    }


}